<template>
  <div class="article-container">
    <el-card class="filter-card">
      <div slot="header" class="clearfix">
        <!-- 面包屑 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 数据表单 -->
      <el-form ref="form" :model="form" label-width="60px" size="mini">
        <el-form-item label="状态：">
          <el-radio-group v-model="status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道：">
          <el-select v-model="channelId" placeholder="请选择频道">
            <el-option label="全部" :value="null"></el-option>
            <el-option
              :label="channel.name"
              :value="channel.id"
              v-for="(channel, index) in channels"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期：">
          <el-date-picker
            v-model="rangeDate"
            type="datetimerange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['12:00:00']"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            :disabled="loadTable"
            type="primary"
            @click="loadArticles(1)"
            >筛选</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>根据筛选条件共查询到{{ totalCount }}条结果：</span>
      </div>
      <!-- 数据列表  表格-->
      <!-- Table 表格使用
        1、把需要展示的数组列表数据绑定给 table 组件的 data属性
           注意： 你不用去 v-for 遍历，他会自己遍历
        2、设计表格列  el-table-column
           width 可以设定表格列的宽度
           label 设定列的标题
           prop 用来设定要渲染的列表项数据字段，只能展示文本
       -->
      <el-table
        :data="articles"
        style="width: 100%"
        stripe
        size="mini"
        class="list-table"
        v-loading="loadTable"
      >
        <el-table-column label="封面">
          <template slot-scope="scope">
            <img
              v-if="scope.row.cover.images[0]"
              class="article-cover"
              :src="scope.row.cover.images[0]"
              alt=""
            />
            <img
              v-else
              class="article-cover"
              src="./12574338a640011.jpg"
              alt=""
            />
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column label="状态">
          <!-- 如果需要在自定义列模板中获取当前遍历项数据，那么就在template上声明 slot-scope='scope' -->
          <template slot-scope="scope">
            <el-tag :type="articleStatus[scope.row.status].type">{{
              articleStatus[scope.row.status].text
            }}</el-tag>
            <!-- v-if 会造成 重排(回流) 不建议使用性能差 尽量避免-->
            <!-- <el-tag v-if="scope.row.status === 0" type="primary">草稿</el-tag>
            <el-tag v-else-if="scope.row.status === 1" type="info"
              >待审核</el-tag
            >
            <el-tag v-else-if="scope.row.status === 2" type="success"
              >审核通过</el-tag
            >
            <el-tag v-else-if="scope.row.status === 3" type="warning"
              >审核失败</el-tag
            >
            <el-tag v-else-if="scope.row.status === 4" type="danger"
              >已删除</el-tag
            > -->
          </template>
        </el-table-column>
        <el-table-column prop="pubdate" label="发布时间"> </el-table-column>
        <el-table-column label="操作">
          <!-- 如果需要自定义表格列模板，则把需要自定义的内容放到 template里面  自定义后prop属性没用-->
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              circle
              icon="el-icon-edit"
              @click="$router.push('/publish?id=' + scope.row.id.toString())"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              circle
              icon="el-icon-delete"
              @click="onDelArticle(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 列表分页 -->
      <el-pagination
        background
        layout="prev, pager, next"
        :total="totalCount"
        :disabled="loadTable"
        :page-size="pageSize"
        :current-page.sync="page"
        @current-change="onCurrentChange"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getArticles, getArticleChannels, delArticle } from '@/api/article'
export default {
  name: 'Article',
  components: {},
  props: {},
  data () {
    return {
      form: {

      },
      articles: [], // 文章数据列表
      totalCount: 0, // 总数据条数
      articleStatus: [
        { status: 0, text: '草稿', type: 'primary' }, // 0
        { status: 1, text: '待审核', type: 'info' }, // 1
        { status: 2, text: '审核通过', type: 'success' }, // 2
        { status: 3, text: '审核失败', type: 'warning' }, // 3
        { status: 4, text: '已删除', type: 'danger' } // 4
      ],
      status: null, // 查询文章的状态，默认不传
      channels: [], // 频道列表
      channelId: null, // 查询文章的频道
      rangeDate: [],
      loadTable: true, // 筛选日期
      page: 1, // 当前页码
      pageSize: 10
    }
  },
  computed: {},
  watch: {},
  mounted () {
    this.loadArticles()
    this.loadArticleChannels()
  },
  methods: {
    // 获取文章列表
    async loadArticles (page = 1) {
      this.loadTable = true
      const { data } = await getArticles({
        page,
        per_page: this.pageSize,
        status: this.status,
        channel_id: this.channelId,
        begin_pubdate: this.rangeDate ? this.rangeDate[0] : null, // 开始事件
        end_pubdate: this.rangeDate ? this.rangeDate[1] : null
      })
      // console.log(data)
      // error  Identifier 'total_count' is not in camel case 没有使用驼峰命名，决绝方法：重命名,total_count: totalCount
      const { results, total_count: totalCount } = data.data
      this.articles = results
      this.totalCount = totalCount
      // console.log(this.totalCount)
      this.loadTable = false // 关闭表格加载
    },
    onSubmit () {
      console.log('submit!')
    },
    onCurrentChange (page) {
      // console.log(page)
      this.loadArticles(page)
    },
    // 获取文章频道列表
    async loadArticleChannels () {
      const { data } = await getArticleChannels()
      // console.log(data)
      this.channels = data.data.channels
    },
    // 删除文章
    onDelArticle (articleId) {
      // console.log(articleId)
      // console.log(articleId.toString())
      this.$confirm('您确定要删除该文章吗？', '提示', {
      }).then(async () => {
        await delArticle(articleId.toString())
        this.loadArticles(this.page)
        this.$message({
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="less" scoped>
.filter-card {
  margin-bottom: 15px;
}
.list-table {
  margin-bottom: 20px;
  /deep/.cell {
    text-align: center;
  }
  .article-cover {
    width: 80px;
    height: 80px;
    background-size: cover;
  }
}
</style>
